﻿using System;
using System.Reflection;
using System.Windows.Forms;

using Pegasus.Diagnostics;

namespace Pegasus.Windows.Forms
{
	/// <summary>
	/// This class show simple message boxes of differnt types.  Error, Info, YesNo, etc...
	/// </summary>
	public class MsgBox
	{
		// Local Static Values
		private static string s_errorCaption = "Error";
		private static string s_nonErrorCaption = "";

		/// <summary>
		/// Initializes the <see cref="MsgBox"/> class.
		/// </summary>
		static MsgBox()
		{
			// In design mode no entry assembly is listed.
			try
			{
				s_nonErrorCaption = Assembly.GetEntryAssembly().GetName().Name.Trim();
			}
			catch
			{
				s_nonErrorCaption = "Application";
			}

			s_errorCaption = s_nonErrorCaption + " Error";
			s_errorCaption = s_errorCaption.Trim();
		}

		/// <summary>
		/// Gets or sets the caption for error message boxes.
		/// </summary>
		/// <value>The caption.</value>
		public static string ErrorCaption
		{
			get
			{
				return s_errorCaption;
			}

			set
			{
				ParamCode.AssertNotEmpty( value, "ErrorCaption" );
				s_errorCaption = value;
			}
		}

		/// <summary>
		/// Gets or sets the caption for non-error message boxes.
		/// </summary>
		/// <value>The caption.</value>
		public static string NonErrorCaption
		{
			get
			{
				return s_nonErrorCaption;
			}

			set
			{
				ParamCode.AssertNotEmpty( value, "NonErrorCaption" );
				s_nonErrorCaption = value;
			}
		}

		/// <summary>
		/// Display a error message box.
		/// </summary>
		/// <param name="msg">The error message.</param>
		public static void ErrorBox( string msg )
		{
			// Check params
			ParamCode.AssertNotEmpty( msg, "msg" );

			MessageBox.Show( msg, s_errorCaption, MessageBoxButtons.OK, MessageBoxIcon.Error );
		}

		/// <summary>
		/// Display a error message box.
		/// </summary>
		/// <param name="format">The error message with format string.</param>
		/// <param name="args">The arguments for the format string.</param>
		public static void ErrorBox( string format, params object[] args )
		{
			// Check params
			ParamCode.AssertNotEmpty( format, "format" );
			ParamCode.AssertNotNull( args, "args" );

			ErrorBox( string.Format( format, args ) );
		}

		/// <summary>
		/// Display a error message box with an exception
		/// </summary>
		/// <param name="msg">The error message.</param>
		/// <param name="e">An exception to display with the error message.</param>
		public static void ErrorBox( string msg, Exception e )
		{
			// Check params
			ParamCode.AssertNotEmpty( msg, "msg" );
			ParamCode.AssertNotNull( e, "e" );

			string formattedMsg = string.Format( "{0}: {1}\n\n{2}", msg, e.Message, e.StackTrace );
			MessageBox.Show( formattedMsg, s_errorCaption, MessageBoxButtons.OK, MessageBoxIcon.Error );
		}

		/// <summary>
		/// Display a error message box with an exception
		/// </summary>
		/// <param name="format">The error message with format string.</param>
		/// <param name="e">An exception to display with the error message.</param>
		/// <param name="args">The arguments for the format string.</param>
		public static void ErrorBox( string format, Exception e, params object[] args )
		{
			// Check params
			ParamCode.AssertNotEmpty( format, "format" );
			ParamCode.AssertNotNull( e, "e" );
			ParamCode.AssertNotNull( args, "args" );

			ErrorBox( string.Format( format, args ), e );
		}


		/// <summary>
		/// Display a information message box.
		/// </summary>
		/// <param name="msg">The message.</param>
		public static void InfoBox( string msg )
		{
			// Check params
			ParamCode.AssertNotEmpty( msg, "msg" );

			MessageBox.Show( msg, s_nonErrorCaption, MessageBoxButtons.OK, MessageBoxIcon.Information );
		}

		/// <summary>
		/// Display a information message box.
		/// </summary>
		/// <param name="format">The message with format string.</param>
		/// <param name="args">The arguments for the format string.</param>
		public static void InfoBox( string format, params object[] args )
		{
			// Check params
			ParamCode.AssertNotEmpty( format, "format" );
			ParamCode.AssertNotNull( args, "args" );

			InfoBox( string.Format( format, args ) );
		}

		/// <summary>
		/// Display a Yes/No question message box.
		/// </summary>
		/// <param name="msg">The message.</param>
		/// <returns>true if the user selected yes or false if the user selected no.</returns>
		public static bool YesNoBox( string msg )
		{
			// Check params
			ParamCode.AssertNotEmpty( msg, "msg" );

			if( MessageBox.Show( msg, s_nonErrorCaption, MessageBoxButtons.YesNo, MessageBoxIcon.Question ) == DialogResult.Yes )
			{
				return true;
			}

			return false;
		}

		/// <summary>
		/// Display a Yes/No question message box.
		/// </summary>
		/// <param name="format">The message with format string.</param>
		/// <param name="args">The arguments for the format string.</param>
		/// <returns>true if the user selected yes or false if the user selected no.</returns>
		public static bool YesNoBox( string format, params object[] args )
		{
			// Check params
			ParamCode.AssertNotEmpty( format, "format" );
			ParamCode.AssertNotNull( args, "args" );

			return YesNoBox( string.Format( format, args ) );
		}
	}
}
